package com.tianji.learning.task;

import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
@RequiredArgsConstructor
@Slf4j
@Component
public class PointsBoardPersistentHandler {
    private final IPointsBoardSeasonService pointsBoardSeasonService;
    /**
     * 创建上赛季 榜单表
     */
    // @Scheduled(cron = "0 0 3 1 * ?")  每月1号，凌晨3点执行 单机版 定时任务调度
    @XxlJob("createTableJob")
    public void createPointsBoardTableOfLastSeason(){
        log.debug("创建上赛季榜单任务执行了");
        // 获取上月当前时间点
        LocalDate time = LocalDate.now().minusMonths(1);
        // 查询赛季id
        PointsBoardSeason one = pointsBoardSeasonService
                .lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, time)
                .ge(PointsBoardSeason::getEndTime, time)
                .one();
        log.debug("上赛季信息 {}",one);
        if (one == null) {
            // 赛季不存在
            return;
        }
        // 创建表
        pointsBoardSeasonService.createPointsBoardLatestTable(one.getId());
    }
}
